/*
 * IC Project - a prototype to test a hypothesis of collective intelligence
 * Copyright (C) 2012 Paolo Conte - paolo@concavi.net
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ix.net;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DBExec extends BaseLocalServlet {

    public String getServletInfo() {
        return "DBExec servlet";
    }

    protected void serve(HttpServletRequest req, HttpServletResponse res) {
        out.println("<h3>Query manager</h3>");
        out.println("<form method=\"post\">");
        out.println("<textarea rows=\"30\" cols=\"90\" name=\"query\"></textarea>");
        out.println("<br/>");
        out.println("<input type=\"submit\" />");
        out.println("</form>");
        out.println("<pre>");
        try {
            String query = req.getParameter("query");
            int rows = 0;
            if (query != null) {
                Connection cnn = (Connection) getServletContext().getAttribute("connection");
                Statement stmt = cnn.createStatement();
                rows = stmt.executeUpdate(query);
                //rows = PicoWeb.exec(query);
            }
            out.println(rows);
        } catch (SQLException sqlexc) {
            out.println(sqlexc);
        }
        out.println("</pre>");
        out.println("</body>");
        out.println("</html>");
    }
}
